package com.csvc.controller.dashboard.cmdb;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csvc.business.cmdb.service.ICmSynOrderService;
import com.csvc.commons.dto.WFMessageDto;
import com.csvc.commons.response.ResponseResult;
import com.csvc.controller.dashboard.Constants;
import com.csvc.repository.cmdb.domain.CmSynOrder;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author vincent zou
 * @since 2021-01-10
 */
@RestController
@RequestMapping("//cm-syn-order")
@Slf4j
public class CmSynOrderController {
	@Resource
	private ICmSynOrderService cmSynOrderService;

	/**
	 * 提交同步申请单
	 *
	 * @param cmSynOrder {@link CmSynOrder}
	 * @return {@link ResponseResult}
	 */
	@PostMapping("submit")
	public ResponseResult create(@Valid @RequestBody CmSynOrder cmSynOrder, BindingResult bindingResult) {
		log.info("---------------CM SYM Oder:{}", cmSynOrder);
		// 表单验证
		if (bindingResult.hasErrors()) {
			return ResponseResult.failure(Objects.requireNonNull(bindingResult.getFieldError()).getDefaultMessage());
		}
		// 业务逻辑
		boolean created = cmSynOrderService.create(cmSynOrder);
		if (created) {
			return ResponseResult.success("提交成功");
		}
		return null;
	}

	/**
	 * 申请单修改
	 *
	 * @param cmSynOrder {@link CmSynOrder}
	 * @return {@link ResponseResult}
	 */
	@PostMapping("update")
	public ResponseResult update(@Valid @RequestBody CmSynOrder cmSynOrder, BindingResult bindingResult) {
		log.info("---------------CM SYM Oder:{}", cmSynOrder);
		// 业务逻辑
		boolean updated = cmSynOrderService.update(cmSynOrder);
		if (updated) {
			return ResponseResult.success("编辑成功");
		}
		return null;
	}

	/**
	 * 加入通知列表
	 * 
	 * @param busiNo
	 * @param contactInfo
	 * @return
	 */
	@GetMapping("putNoti/{busiNo}/{contactInfo}")
	public ResponseResult putIntoNotificaton(@PathVariable String busiNo, @PathVariable String contactInfo) {
		log.info("busiNo:{},contactInfo:{}", busiNo, contactInfo);
		boolean flag = cmSynOrderService.putNotificationList(contactInfo, busiNo);
		if (flag) {
			// TODO email to contactInfo. decide to configure it in workflow.
			return ResponseResult.success("加入通知成功!");
		}
		return ResponseResult.failure("加入通知失败!");
	}

	/**
	 * 包含条件检查 TODO
	 * 
	 * @param busiNo
	 * @return
	 */
	@GetMapping("checkIncluCond/{busiNo}/{isEdit}")
	public ResponseResult checkInclusiveCondition(@PathVariable String busiNo, boolean isEdit) {
		log.info("busiNo:{},isEdit:{}", busiNo, isEdit);
		
		CmSynOrder order = cmSynOrderService.checkInclusiveConditions(busiNo);
		Map<String, String> map = new HashMap<>();
		if(null == order) {
			map.put("status", "0"); // 0 : no same 1 : same in-progress 2: same history 3: same history+ reSubmit
			map.put("busiNo", "0");
		}else {
			if(order.getStatus() == 0) {
				map.put("status", "1");
				map.put("busiNo", order.getBusiNo());
			}else{
				map.put("status", "2");
				map.put("busiNo", order.getBusiNo());
			};
		}
		return ResponseResult.success(map);
	}

	/**
	 * TODO 获取数据库信息
	 * 
	 * @param busiNo
	 * @return
	 */
	@GetMapping("getDataInfo/{busiNo}")
	public ResponseResult getDataInfo(@PathVariable String busiNo) {
		log.info("busiNo:{}", busiNo);
		Map<String, Object> map = cmSynOrderService.getDataInfo(busiNo);
	//	CmSynOrder order = cmSynOrderService.checkInclusiveConditions(busiNo);
		//map.put("DataInfo", "database1-------");
		return ResponseResult.success(map);

	}

	@GetMapping("checkMaxCond/{busiNo}")
	public ResponseResult checkMaxCondition(@PathVariable String busiNo) {
		log.info("busiNo:{},contactInfo:{}", busiNo);
		Map<String, String> map = new HashMap<>();
		map.put("isHaveMax", "true");
		return ResponseResult.success(map);
		// return ResponseResult.failure("check inclusive condition failure!");
	}

	/**
	 * 删除
	 *
	 * @param cmSynOrderId {@code Long}
	 * @return {@link ResponseResult}
	 */
	@DeleteMapping("remove/{cmSynOrderId}")
	public ResponseResult remove(@PathVariable Long cmSynOrderId) {
		// 业务逻辑
		boolean deleted = cmSynOrderService.remove(cmSynOrderId);
		if (deleted) {
			return ResponseResult.success("删除成功");
		}

		return null;
	}

	/**
	 * 获取
	 *
	 * @param cmSynOrderId {@code Long}
	 * @return {@link ResponseResult}
	 */
	@GetMapping("get/{cmSynOrderId}")
	public ResponseResult get(@PathVariable Long cmSynOrderId) {
		CmSynOrder cmSynOrder = cmSynOrderService.get(cmSynOrderId);
		return ResponseResult.success(cmSynOrder);
	}

	/**
	 * 结果通知
	 *
	 * @param cmSynOrder {@link CmSynOrder}
	 * @return {@link ResponseResult}
	 */
	@PostMapping("noti")
	public ResponseResult notifyCompletion(@RequestBody WFMessageDto msg) {
		log.info("Message:{}", msg);
		cmSynOrderService.archiveOrder(msg);
		return ResponseResult.success("处理成功");

	}

	/**
	 * 分页
	 *
	 * @param current {@code int} 页码
	 * @param size    {@code int} 笔数
	 * @return {@link ResponseResult}
	 */
	@GetMapping("page")
	public ResponseResult page(@RequestParam int current, @RequestParam int size,
			@ModelAttribute CmSynOrder cmSynOrder) {
		IPage<CmSynOrder> page = cmSynOrderService.page(current, size, cmSynOrder);
		return ResponseResult.success(page);
	}
	@GetMapping("geneScripts/{busiNo}")
	public ResponseResult generateScripts(@PathVariable String busiNo) {
		log.info("busiNo:{}", busiNo);
		boolean flag = true; //
		Map<String, Object> map = cmSynOrderService.geneScripts(busiNo);
		if (flag) {
			return ResponseResult.success(map);
		}
		// call notification
		return ResponseResult.failure("产生脚本失败!");
	}
	
}



/**
 * 前提(你可以查询下面的数据库信息表,我有提供目录)
1.Oracle数据库创建EXP_EXCEL_DIR,指定备份路径, 确保表空间一致
2.Mysql数据库指定备份路径

Oracle
Schema迁移
expdp "userid='/ as sysdba'" directory=exp_excel_dir schemas={source_schema} dumpfile={source_schema}.dmp CONTENT=all
覆盖导入
impdp "userid='/ as sysdba'" directory=exp_excel_dir  dumpfile={source_schema}.dmp remap_schema={source_schema}:{target_schema} table_exists_action=REPLACE
追加导入
impdp "userid='/ as sysdba'" directory=exp_excel_dir  dumpfile={source_schema}.dmp remap_schema={source_schema}:{target_schema} table_exists_action=APPEND

Table迁移
expdp "userid='/ as sysdba'" directory=exp_excel_dir dumpfile={source_schema.table_name1}.dmp tables={source_schema.table_name1}
覆盖导入
impdp "userid='/ as sysdba'" directory=exp_excel_dir  dumpfile={source_schema.table_name1}.dmp remap_schema={source_schema}:{target_schema} table_exists_action=REPLACE
追加导入
impdp "userid='/ as sysdba'" directory=exp_excel_dir  dumpfile={source_schema.table_name1}.dmp remap_schema={source_schema}:{target_schema} remap_table={source_schema.table_name1}:{target_table_name1} table_exists_action=APPEND

Table partition迁移
expdp "userid='/ as sysdba'" directory=exp_excel_dir dumpfile={source_schema.table_name1}.dmp tables={source_schema.table_name1:p210101}
覆盖导入
impdp "userid='/ as sysdba'" directory=exp_excel_dir  dumpfile={source_schema.table_name1}.dmp remap_schema={source_schema}:{target_schema} remap_table={source_schema.table_name1}:{target_table_name1} table_exists_action=REPLACE
追加导入
impdp "userid='/ as sysdba'" directory=exp_excel_dir  dumpfile={source_schema.table_name1}.dmp remap_schema={source_schema}:{target_schema} remap_table={source_schema.table_name1}:{target_table_name1} table_exists_action=APPEND


MySQL
整个实例迁移
mysqldump --single-transaction --set-gtid-purged=OFF --all-databases --triggers --routines --events >all_databases.sql
mysql < all_databases.sql

schema迁移
mysqldump --single-transaction --set-gtid-purged=OFF {source_database} >{database_name}.sql
mysql {target_database} <{database_name}.sql

table迁移
mysqldump --single-transaction --set-gtid-purged=OFF {source_database} {table_name} >{table_name}.sql
mysql {target_database} <{table_name}.sql
 */
